﻿using Assignment_3.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment_3.Feature___Housekeeping
{
    public partial class RetrieveHousekeeping : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }
        }

        protected void tbnRetrieve_Click(object sender, EventArgs e)
        {
            string schedule = tbxSchedule.Text;
            lblCheck.Visible = false;

            int id = Convert.ToInt32(tbxSchedule.Text);

            if (DatabaseManager.compareHouseKeepingID(id) == false)
            {
                lblCheck.Visible = true;
                lblCheck.Text = "There is no Schedule with that ID!";

                lblScheduleID.Visible = false;
                lblStaffID.Visible = false;
                lblName.Visible = false;
                lblDuty.Visible = false;
                lblRoomID.Visible = false;
                lblDateOfDuty.Visible = false;
                lblProgress.Visible = false;
                lblRoomStatus.Visible = false;

                tbnUpdate.Visible = false;
                tbnDelete.Visible = false;
                tbnProgress.Visible = false;
            }

            else
            {
                Object[] housekeeping = DatabaseManager.GetHouseKeepingById(id);
                lblCheck.Visible = false;

                lblScheduleID.Visible = true;
                lblStaffID.Visible = true;
                lblName.Visible = true;
                lblDuty.Visible = true;
                lblRoomID.Visible = true;
                lblDateOfDuty.Visible = true;
                lblProgress.Visible = true;
                lblRoomStatus.Visible = true;

                lblScheduleID.Text = Convert.ToString((int)housekeeping[0]);
                lblStaffID.Text = Convert.ToString((int)housekeeping[1]);
                lblName.Text = (string)housekeeping[2];
                lblDuty.Text = (string)housekeeping[3];
                lblRoomID.Text = Convert.ToString((int)housekeeping[4]);
                DateTime date = (DateTime)housekeeping[5];
                lblDateOfDuty.Text = string.Format("{0:yyyy-MM-dd}", date);
                lblProgress.Text = (string)housekeeping[6];
                lblRoomStatus.Text = (string)housekeeping[7];

                tbnUpdate.Visible = false;
                tbnDelete.Visible = true;
                tbnProgress.Visible = false;

                if (lblProgress.Text.Equals("In Progress"))
                {
                    tbnUpdate.Visible = true;
                    tbnProgress.Visible = true;
                }
            }

        }

        protected void tbnUpdate_Click(object sender, EventArgs e)
        {
            Server.Transfer("UpdateSchedule.aspx?housekeepingID=" + lblScheduleID.Text);
        }

        protected void tbnDelete_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(lblScheduleID.Text);
            string scheduleID = Convert.ToString(id);
            DatabaseManager.DeleteHouseKeeping(id);
            Server.Transfer("DeleteSchedule.aspx?housekeepingID=" + scheduleID);
        }

        protected void tbnProgress_Click(object sender, EventArgs e)
        {
            int sID = Convert.ToInt32(lblScheduleID.Text);
            int rID = Convert.ToInt32(lblRoomID.Text);
            string scheduleID = Convert.ToString(sID);

            HouseKeeping schedule = new HouseKeeping();
            schedule.HouseKeepingID = (sID);
            schedule.Progress = "Completed";

            Room r = new Room();
            r.RoomID = (rID);
            r.Status = "Vacant";

            if (DatabaseManager.UpdateHouseKeepingProgress(schedule) != 0 && DatabaseManager.UpdateRoomStatus(r) != 0)
            {
                Server.Transfer("UpdateScheduleProgress.aspx?housekeepingID=" + scheduleID);
            }

        }
    }
}